package ldh.im.fxgui.controller;

import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.concurrent.Task;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.Pane;
import javafx.scene.layout.Priority;
import javafx.scene.layout.VBox;
import ldh.im.dto.enums.MessageType;
import ldh.im.fxbase.dao.ImCollectDao;
import ldh.im.fxbase.data.pojo.ImCollect;
import ldh.im.fxbase.util.BeanFactory;
import ldh.im.fxbase.util.StringUtil;
import ldh.im.fxbase.util.ThreadPoolUtil;
import ldh.im.fxgui.component.cell.CollectView;
import ldh.im.fxgui.util.UiUtil;
import org.fxmisc.flowless.Cell;
import org.fxmisc.flowless.VirtualFlow;
import org.fxmisc.flowless.VirtualizedScrollPane;

import java.net.URL;
import java.util.Arrays;
import java.util.List;
import java.util.ResourceBundle;
import java.util.function.BiFunction;

public class CollectController implements Initializable {

    @FXML private TextField searchTextField;
    @FXML private Label titleLabel;
    @FXML private Pane contentPane;

    private ObservableList<ImCollect> collectObservableList = FXCollections.observableArrayList();

    public void remove(ImCollect imCollect) {
        collectObservableList.remove(imCollect);
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        showAllCollectAct(null);
    }

    @FXML
    private void searchAct(ActionEvent actionEvent) {
        String search = searchTextField.getText().trim();
        if(StringUtil.isEmpty(search)) {
            loadData("全部收藏", null,null);
        } else {
            loadData(String.format("搜索\"%s\"收藏", search), search,null);
        }
    }

    @FXML
    private void showAllCollectAct(ActionEvent actionEvent) {
        loadData("全部收藏", null,null);
    }

    @FXML
    private void showLinkCollectAct(ActionEvent actionEvent) {
        loadData("链接", null, Arrays.asList(MessageType.Url));
    }

    @FXML
    private void showPictureCollectAct(ActionEvent actionEvent) {
        loadData("图片与视频", null, Arrays.asList(MessageType.Image, MessageType.Video));
    }

    @FXML
    private void showNotesAct(ActionEvent actionEvent) {

    }

    @FXML
    private void showFileCollectAct(ActionEvent actionEvent) {
        loadData("文件", null, Arrays.asList(MessageType.File));
    }

    @FXML
    private void showChatAct(ActionEvent actionEvent) {
        loadData("语音", null, Arrays.asList(MessageType.Text, MessageType.RichText, MessageType.Web, MessageType.Html));
    }

    @FXML
    private void showAudioCollectAct(ActionEvent actionEvent) {
        loadData("语音", null, Arrays.asList(MessageType.Audio));
    }

    private VirtualizedScrollPane createContentPane(ObservableList<ImCollect> collectObservableList, BiFunction<CollectController, ImCollect, Node> nodeCreator) {
        VirtualFlow messageSessionList = VirtualFlow.createVertical(collectObservableList, m->{
            Node node = nodeCreator.apply(CollectController.this, m);
            return Cell.wrapNode(node);
        });

        VirtualizedScrollPane virtualizedScrollPane = new VirtualizedScrollPane<>(messageSessionList);
        return virtualizedScrollPane;
    }

    private void loadData(String name, String search,  List<MessageType> messageTypes) {
        titleLabel.setText(name);
        collectObservableList.clear();
        VirtualizedScrollPane contentNode = createContentPane(collectObservableList, CollectView::new);
        VBox.setVgrow(contentNode, Priority.ALWAYS);
        contentPane.getChildren().setAll(contentNode);

        Task<List<ImCollect>> task = new Task() {

            @Override
            protected Object call() throws Exception {
                String userId = UiUtil.getLoginImUser().getUserId();

                ImCollectDao collectDao = BeanFactory.getInstance().getBean(ImCollectDao.class);
                List<ImCollect> collectList = collectDao.getCollects(userId, search, messageTypes,500);
                return collectList;
            }
        };
        ThreadPoolUtil.submitTask(task, "获取收藏");
        task.setOnSucceeded(s->{
            if (task.getValue().size() < 1) return;
            collectObservableList.setAll(task.getValue());
            VirtualFlow virtualFlow = (VirtualFlow) contentNode.getContent();
            virtualFlow.show(0);
        });
    }
}
